home *** CD-ROM | disk | FTP | other *** search
/ Workbench Add-On / Workbench Add-On - Volume 1.iso / Dev / Oberon / source / Obsolete / Args.mod next >
Encoding:
Text File  |  1995-06-29  |  4.3 KB  |  156 lines

  1. (*************************************************************************
  2.  
  3.      $RCSfile: Args.mod $
  4.   Description: C-style command-line argument parsing
  5.  
  6.    Created by: fjc (Frank Copeland)
  7.     $Revision: 2.8 $
  8.       $Author: fjc $
  9.         $Date: 1995/06/29 19:06:56 $
  10.  
  11.   Copyright © 1994-1995, Frank Copeland.
  12.   This file is part of the Oberon-A Library.
  13.   See Oberon-A.doc for conditions of use and distribution.
  14.  
  15. *************************************************************************)
  16.  
  17. <* STANDARD- *>
  18.  
  19. MODULE Args;
  20.  
  21. IMPORT SYS := SYSTEM, Kernel, e := Exec, d := Dos, wb := Workbench;
  22.  
  23. TYPE
  24.   ArgVPtr *= POINTER TO ARRAY OF e.LSTRPTR;
  25.  
  26. VAR
  27.  
  28.   IsCLI -     : BOOLEAN;
  29.     (*
  30.      * TRUE = program started from CLI, FALSE = program started from
  31.      * Workbench
  32.      *)
  33.  
  34.   argc -      : LONGINT;
  35.     (* Number of arguments passed by CLI *)
  36.  
  37.   argv -      : ArgVPtr;
  38.     (*
  39.      * Array of argument strings passed by CLI.
  40.      * argv [0] is always the name of the program.
  41.      *)
  42.  
  43.   NumArgs -   : LONGINT;
  44.     (* Number of arguments passed by Workbench *)
  45.  
  46.   ArgList -   : wb.WBArgumentsPtr;
  47.     (* Array of WBArg structures passed by Workbench *)
  48.  
  49.   DosCmdLen - : LONGINT;
  50.     (* Length of the command string passed by the CLI *)
  51.  
  52.   DosArgs -   : e.LSTRPTR;
  53.     (* The actual command string passed by the CLI; !! DO NOT CHANGE !! *)
  54.  
  55.   argCopy     : e.LSTRPTR;
  56.     (* Copy of the command string passed by the CLI *)
  57.  
  58. (*------------------------------------*)
  59. PROCEDURE CliArgs ();
  60.  
  61.   VAR index, index2 : LONGINT; nameLen : INTEGER;
  62.       process : d.ProcessPtr; prCLI : d.CommandLineInterfacePtr;
  63.  
  64. BEGIN (* CliArgs *)
  65.   IsCLI := TRUE; NumArgs := 0; ArgList := NIL;
  66.   argCopy := NIL; argc := 1; (* First arg is always program name *)
  67.   DosCmdLen := Kernel.dosCmdLen;
  68.   DosArgs := SYS.VAL (e.LSTRPTR, Kernel.dosCmdBuf);
  69.  
  70.   IF DosCmdLen > 0 THEN
  71.     (* Make a copy of the command string *)
  72.     SYS.NEW (argCopy, SYS.STRLEN (DosArgs^) + 1);
  73.     COPY (DosArgs^, argCopy^);
  74.  
  75.     (*
  76.      * Scan the copy, planting a NUL at the end of each argument and
  77.      * keeping a count of the arguments found;
  78.      *)
  79.     index := 0;
  80.     LOOP
  81.       IF index >= DosCmdLen THEN (* last argument found *) EXIT END;
  82.       (* Kill any leading spaces *)
  83.       WHILE argCopy [index] = " " DO
  84.         argCopy [index] := 0X; INC (index)
  85.       END;
  86.       IF argCopy [index] = 22X THEN (* a quoted argument *)
  87.         (* zap the quote *)
  88.         argCopy [index] := 0X; INC (index);
  89.         (* scan for the next quote *)
  90.         WHILE argCopy [index] # 22X DO INC (index) END;
  91.         (* zap it too *)
  92.         argCopy [index] := 0X; INC (index);
  93.         INC (argc)
  94.       ELSIF argCopy [index] > " " THEN (* an unquoted argument *)
  95.         (* scan for the end of the argument *)
  96.         WHILE argCopy [index] > " " DO INC (index) END;
  97.         (* mark it *)
  98.         argCopy [index] := 0X; INC (index);
  99.         INC (argc)
  100.       ELSE
  101.         (*
  102.          * This is probably a dummy "\n" at the end of an empty command
  103.          * line.
  104.          *)
  105.         argCopy [index] := 0X; INC (index)
  106.       END; (* IF *)
  107.     END; (* LOOP *)
  108.  
  109.     (* Allocate the argv array *)
  110.     NEW (argv, argc + 1);
  111.  
  112.     IF argc > 0 THEN (* Fill argv with pointers to arguments *)
  113.       index := 0; index2 := 1;
  114.       WHILE index2 < argc DO
  115.         WHILE argCopy [index] = 0X DO INC (index) END;
  116.         argv [index2] := SYS.ADR (argCopy [index]);
  117.         INC (index2);
  118.         WHILE argCopy [index] # 0X DO INC (index) END
  119.       END; (* WHILE *)
  120.     END; (* IF *)
  121.  
  122.     (* Terminate it with a NIL *)
  123.     argv [argc] := NIL
  124.   ELSE
  125.     (* Create a dummy entry for argv *)
  126.     NEW (argv, 2);
  127.     argv [1] := NIL
  128.   END; (* ELSE *)
  129.  
  130.   (* Get the command name *)
  131.   process := SYS.VAL (d.ProcessPtr, e.FindTask (e.NILSTR));
  132.   nameLen := ORD (process.cli.commandName [0]);
  133.   Kernel.Allocate (argv [0], nameLen + 1, {e.memClear});
  134.   SYS.MOVE (SYS.ADR (process.cli.commandName [1]), argv [0], nameLen);
  135.   (* argv [0, nameLen] := 0X; *)
  136. END CliArgs;
  137.  
  138. (*------------------------------------*)
  139. PROCEDURE WBArgs ();
  140.  
  141.   VAR wbMsg : wb.WBStartupPtr;
  142.  
  143. BEGIN (* WBArgs *)
  144.   IsCLI := FALSE; argc := 0; argv := NIL;
  145.   DosCmdLen := 0; DosArgs := NIL; argCopy := NIL;
  146.   wbMsg := Kernel.WBenchMsg;
  147.   NumArgs := wbMsg.numArgs; ArgList := wbMsg.argList
  148. END WBArgs;
  149.  
  150. BEGIN (* Args *)
  151.   IF Kernel.fromWorkbench THEN WBArgs()
  152.   ELSE CliArgs()
  153.   END
  154. END Args.
  155.  
  156.